#pragma once

#include <QSound>
#include <QTimer>
#include "Block.h"

// 游戏
class Game: public QObject {
    Q_OBJECT

    // 状态
    public: enum Status {
        END,        // 已结束
        PAUSED,     // 暂停中
        PLAYING,    // 游戏中
    };

    signals: void UM_DIFFICULTY_UP(int);    // 难度增加事件
    signals: void UM_GAME_OVER(bool);       // 游戏结束事件
    signals: void UM_SCORE(int);            // 得分事件

    private: Block* mCurBlock;          // 当前方块
    private: int mDifficulty;           // 难度
    private: QPainter mDrawer;          // 绘图者
    private: bool mEnableMusic;         // 音乐开关
    private: QWidget* mGameBoard;       // 游戏区域
    private: QPixmap* mGameBoardBitmap; // 游戏区域后台缓冲位图
    private: QSound mMusicPlayer;       // 音乐播放者
    private: Block* mNextBlock;         // 下一个方块
    private: QWidget* mPreview;         // 预览区域
    private: QPixmap* mPreviewBitmap;   // 预览区域后台缓冲位图
    private: int mScore;                // 得分
    private: Status mStatus = END;      // 状态
    private: QTimer mTimer;             // 定时器
    private: int mTop;                  // 有方块的最高行
    public: static ColorGrid sExistentBlocks[Const::GAME_BOARD_GRID_COUNT_V][Const::GAME_BOARD_GRID_COUNT_H];   // 累积的方块

    public: Game(QWidget*, QPixmap*, QWidget*, QPixmap*);
    public: ~Game();
    private: void checkRow();
    private: Block* createRandBlock();
    private: void drawExistentBlock();
    private: void drawGameBoard();
    private: void drawPreview();
    private: void gameOver(bool);
    public: Status getStatus();
    private: bool isLost();
    private: bool isWon();
    private: void moveBlockDown();
    private: void nextBlock();
    public: void onKeyPressed(int);
    private slots: void onTimer();
    public: void pause();
    public: bool playMusic();
    private: void releaseRow(int);
    public: void start();
    private: void stop();
};
